﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.ApplicationPages;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

namespace zevenseas.TimerJobOverview.ApplicationPages
{
    public class EditJob : OperationsPage
    {
        protected EncodedLiteral EncodedLiteral2;
        protected SchedulePicker schedulePicker;
        protected WebApplicationSelector webApplicationSelector;
        protected ServiceSelector serviceSelector;

        protected InputFormTextBox uxJobTitle;
        protected InputFormTextBox uxJobName;
        protected InputFormCheckBox uxJobDisabled;

        private string currentView;
        private SPJobDefinitionCollection jobDefinitions;
        private Guid timerJobId;

        protected override void CreateChildControls()
        {
            if (Page.Request.QueryString["timerjobid"] != null)
            {
                if (!Page.IsPostBack)
                {
                    SPJobDefinition jobDefinition = GetJobDefinition();
                    if (jobDefinition != null)
                    {
                        EncodedLiteral2.Text = (jobDefinition.TypeName.Contains("Microsoft")) ? "You are currently editing a Microsoft Timer Job, please be careful" : "Edit Timer Job";

                        uxJobTitle.Text = jobDefinition.Title.ToString();
                        uxJobName.Text = jobDefinition.Name.ToString();
                        uxJobDisabled.Checked = jobDefinition.IsDisabled;
                        schedulePicker.ScheduleString = jobDefinition.Schedule.ToString();
                    }
                }
            }

            base.CreateChildControls();
        }

        public void OnClickOK(object sender, EventArgs e)
        {
            try
            {
                SPJobDefinition jobDefinition = GetJobDefinition();
                jobDefinition.Title = uxJobTitle.Text;
                jobDefinition.Name = uxJobName.Text;
                jobDefinition.IsDisabled = uxJobDisabled.Checked;
                jobDefinition.Schedule =
                        (schedulePicker.IsValid) ? schedulePicker.Schedule : jobDefinition.Schedule;
                jobDefinition.Update();

                SPUtility.Redirect("/_admin/zevenseas.TimerJobOverview/timerjoboverview.aspx", SPRedirectFlags.Default, this.Context);
            }
            catch (Exception error)
            {
                SPUtility.TransferToErrorPage(error.Message.ToString());
            }            
        }


        private SPJobDefinition GetJobDefinition()
        {
            SPJobDefinition jobDefinition = null;
            currentView = Page.Request.QueryString["View"];
            timerJobId = new Guid(Page.Request.QueryString["timerjobid"].ToString());

            switch (currentView)
            {
                case "Service":
                    jobDefinitions = serviceSelector.CurrentItem.JobDefinitions;
                    break;

                case "WebApplication":
                    jobDefinitions = webApplicationSelector.CurrentItem.JobDefinitions;
                    break;

                case "Farm":
                    foreach (SPService service in SPFarm.Local.Services)
                    {
                        if (service.JobDefinitions.GetValue<SPJobDefinition>(timerJobId) != null)
                        {
                            jobDefinitions = service.JobDefinitions;
                            break;
                        }
                    }
                    break;
                     
            }

            return jobDefinition = jobDefinitions[timerJobId];
        }
    }


}
